package cc.rico.es.utils;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Author: Dell
 * @Date: 2019/1/28
 * @Description:
 */
public class TransportClientUtil {
    private static final String DEFAULT_HOST = "192.168.1.180";
    private static final int DEFAULT_PORT = 9300;

    public static TransportClient client() throws UnknownHostException {
        Settings settings = Settings.builder()
//                .put("cluster.name", "elasticsearch")
//                .put("client.transport.sniff", true)
//                .put("client.transport.ignore_cluster_name", false)
//                .put("client.transport.ping_timeout", 5)
//                .put("client.transport.nodes_sampler_interval", 5)
                .build();

        TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(DEFAULT_HOST), DEFAULT_PORT));
        return client;
    }

    public static void shutdown(TransportClient client) {
        client.close();
    }
}
